<template>
  <div class="article-detail__container">
    <!-- Header 区域 -->
    <van-nav-bar fixed title="文章详情" left-arrow @click-left="$router.back()" />

   <div class="main_container" v-if="!loading">
      <!-- 文章信息区域 -->
    <div class="article-container">
      <!-- 文章标题 -->
      <h1 class="art-title">{{artObj.title}}</h1>

      <!-- 用户信息 -->
      <van-cell center :title="artObj.aut_name" :label="$time(artObj.pubdate)">
        <template #icon>
          <img :src="artObj.aut_photo" :alt="artObj.aut_name" class="avatar">
        </template>
        <template #default>
          <div>
            <van-button
            type="info"
            size="mini"
            v-if="artObj.is_followed"
            @click="followOrUnfollow"
            >已关注</van-button>
            <van-button
            icon="plus"
            type="info"
            size="mini"
            plain
            v-else
            @click="followOrUnfollow"
            >关注</van-button>
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider></van-divider>

      <!-- 文章内容 -->
      <div class="art-content" v-html="artObj.content">好好学习, 天天向上</div>

      <!-- 分割线 -->
      <van-divider>End</van-divider>

      <!-- 点赞 -->
      <div class="like-box">
        <van-button
        icon="good-job"
        type="danger"
        v-if="artObj.attitude == 1"
        size="small"
        @click="likeOrDislike"
        >已点赞</van-button>
        <van-button
        icon="good-job-o"
        type="danger"
        plain
        size="small"
        v-else
        @click="likeOrDislike"
        >点赞</van-button>
      </div>
    </div>
    <!-- 评论 -->
    <Comment/>
   </div>
  </div>
</template>

<script>
import { getArticleDetailAPI, followAutAPI, unfollowAutAPI, likeAPI, disLikeAPI } from '@/api/article'
import Comment from './Comment'
export default {
  name: 'ArticleDtail',
  components: { Comment },
  data () {
    return {
      loading: true,
      artObj: {} // 文章详情对象
    }
  },
  async created () {
    // 实现加载过程中的loading效果
    const loadingBox = this.$toast.loading({
      duration: 0, // 持续展示 toast
      forbidClick: false,
      message: '加载中...'
    })
    // 文章详情获取
    try {
      const res = await getArticleDetailAPI({
        artId: this.$route.query.art_id
      })
      this.artObj = res.data
      // 加载过程优化显示
      this.loading = false // 主体区域盒子显示
    } catch (error) {
      this.$router.replace('/layout')
    } finally {
      loadingBox.clear() // 清除加载状态
    }
  },
  methods: {
    // 关注功能
    async followOrUnfollow () {
      try {
        // 同步到后台
        this.artObj.is_followed ? await unfollowAutAPI({
          autId: this.artObj.aut_id
        }) : await followAutAPI({
          autId: this.artObj.aut_id
        })
        // 先提示!!! 先根据目前状态提示信息,然后再改变状态,重新赋值
        this.$toast.success(this.artObj.is_followed ? '取关成功' : '关注成功')
        // 同步到Vue变量
        this.artObj.is_followed = !this.artObj.is_followed
      } catch (error) {
        this.$toast.success(this.artObj.is_followed ? '取关失败' : '关注失败')
      }
    },
    // 点赞功能
    async likeOrDislike () {
      try {
        // 同步到后台
        this.artObj.attitude === 1 ? await disLikeAPI({
          artId: this.artObj.art_id
        }) : await likeAPI({
          artId: this.artObj.art_id
        })
        // 先提示!!! 先根据目前状态提示信息,然后再改变状态,重新赋值
        this.$toast.success(this.artObj.attitude === 1 ? '取消点赞' : '已点赞')
        // 同步到Vue变量
        this.artObj.attitude = this.artObj.attitude === 1 ? -1 : 1
      } catch (error) {
        this.$toast.fail(this.artObj.attitude === 1 ? '取消点赞失败' : '点赞失败')
      }
    }
  }

}
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
    /deep/ img{
    width: 100%;
  }
  /deep/ pre {
      white-space: pre-wrap;
      word-wrap: break-word;
  }
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
</style>
